package com.cjh.scala

/**
 * Created by Administrator on 2016/6/10 0010.
 */
object Basic2 {
  def main(args: Array[String]) {
    //    var p=new Person //创建对象是可以不加括号
    //    p.name="Bob"
    //    println(p.name+":::"+p.sex+"::")
    //    val p2=new Person2("bob",22)
    //    p2.name="Bob"
    //    println(p2.name)

    //    var p3 = new Person3("Bob", 23, "male")
    val s = new Student("Bob", 22, "java")
    println(s.toString)
  }

}

class Person {
  var name: String = _
  //_相当于一个placeholder，此时必须赋类型
  val sex = "male"
  private[this] val age = "22" //private[this] 表示仅供内部对象使用
}

//主构造器直接跟来类名后面
//类创建时候会执行类中所有语句
//构造器中不加val和var修饰的字段私有
class Person2(var name: String, age: Int) {
  println("i was created!!!")
}

class Person3(var name: String, var age: Int) {
  println("this is person3!!!")
  var gender: String = _

  //子构造器名称为this
  //子构造器必须首先调用父构造器或其他已经存在的构造器
  def this(name: String, age: Int, gender: String) {
    this(name, age)
    this.gender = gender
  }
}

//继承用关键字extends,父类中已经有的字段不用加var或val
class Student(name: String, age: Int, var major: String) extends Person3(name, age) {
  println("this is subclass!!!")

  override def toString: String = "override!!!" //子类重写父类的方法必须加上override
}